package com.example.stmp.Exception;

import com.example.stmp.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.validation.ConstraintViolationException;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 全局异常处理器
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理所有未捕获的异常
     *
     * @param ex 异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<Result> handleException(Exception ex, WebRequest request) {
        // 记录异常信息
        log.error("发生异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(500, "系统错误: " + ex.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理运行时异常
     *
     * @param ex 运行时异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseEntity<Result> handleRuntimeException(RuntimeException ex, WebRequest request) {
        // 记录异常信息
        log.error("发生运行时异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(400, "运行时错误: " + ex.getMessage(), null), HttpStatus.BAD_REQUEST);
    }

    /**
     * 处理非法参数异常
     *
     * @param ex 非法参数异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ResponseEntity<Result> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
        // 记录异常信息
        log.error("非法参数异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(400, "非法参数: " + ex.getMessage(), null), HttpStatus.BAD_REQUEST);
    }

    /**
     * 处理空指针异常
     *
     * @param ex 空指针异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ResponseEntity<Result> handleNullPointerException(NullPointerException ex, WebRequest request) {
        // 记录异常信息
        log.error("空指针异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(400, "空指针: " + ex.getMessage(), null), HttpStatus.BAD_REQUEST);
    }

    /**
     * 处理数据访问异常
     *
     * @param ex 数据访问异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(DataAccessException.class)
    @ResponseBody
    public ResponseEntity<Result> handleDataAccessException(DataAccessException ex, WebRequest request) {
        // 记录异常信息
        log.error("数据访问异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(500, "数据访问错误: " + ex.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理SQL异常
     *
     * @param ex SQL异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(SQLException.class)
    @ResponseBody
    public ResponseEntity<Result> handleSQLException(SQLException ex, WebRequest request) {
        // 记录异常信息
        log.error("SQL异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(500, "SQL错误: " + ex.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 处理方法参数验证异常
     *
     * @param ex 方法参数验证异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseEntity<Result> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, WebRequest request) {
        // 记录异常信息
        log.error("方法参数验证异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(400, "参数验证错误: " + ex.getBindingResult().getFieldError().getDefaultMessage(), null), HttpStatus.BAD_REQUEST);
    }

    /**
     * 处理约束验证异常
     *
     * @param ex 约束验证异常对象
     * @param request Web请求对象
     * @return 包含错误信息的ResponseEntity
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public ResponseEntity<Result> handleConstraintViolationException(ConstraintViolationException ex, WebRequest request) {
        // 记录异常信息
        log.error("约束验证异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(400, "约束验证错误: " + ex.getMessage(), null), HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(IOException.class)
    @ResponseBody
    public ResponseEntity<Result> IOException(IOException ex, WebRequest request) {
        // 记录异常信息
        log.error("约束验证异常: {}", ex.getMessage(), ex);
        // 返回错误信息和HTTP状态码
        return new ResponseEntity<>(new Result(400, "处理文件读写、网络通信等 I/O 操作异常: " + ex.getMessage(), null), HttpStatus.BAD_REQUEST);
    }
}
